package com.quasar.hpatient.presenter;

import com.quasar.hpatient.model.custommodel.RemindMedicineTableItem;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.MedicationInfoDatadb;
import com.quasar.hpatient.model.medicalmodel.MedicationRecordData;
import com.quasar.hpatient.model.medicalmodel.OnedayMedicationData;
import com.quasar.hpatient.model.requestmodel.RequestModel;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.onInterface.OnMedicationListener;
import com.vise.log.ViseLog;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/10/26 0026
 * View:
 */

public class RemindMedicinePresenter {

    private OnMedicationListener onMedicationListener;
    private RequestModel requestModel;

    public RemindMedicinePresenter(OnMedicationListener onMedicationListener) {
        this.requestModel = new RequestModel();
        this.onMedicationListener = onMedicationListener;
    }

    public void PostRemindMedicine(){
        requestModel.MedicationRemind(new AnotherResultListener<List<RemindMedicineTableItem>>() {
            @Override
            public void onSuccess(List<RemindMedicineTableItem> data) {
            }

            @Override
            public void onError(String msg) {

            }

            @Override
            public void onRefreshUI() {

            }
        });
    }

    /**
     * 获取当日服药单信息
     * @param recordTime 查询时间
     */
    public void GetPatientSingleDoseDetailData(String recordTime){

        requestModel.GetPatientSingleDoseDetailData(recordTime, new AnotherResultListener<AnotherResult<OnedayMedicationData>>() {
            @Override
            public void onSuccess(AnotherResult<OnedayMedicationData> data) {
                onMedicationListener.onSuccess(data);

                //天假之前删除表里面的内容

                DataSupport.deleteAll(MedicationInfoDatadb.class);

                if(data.getList()!=null && data.getList().size()>0){
                    int size=data.getList().size();
                    for(int i=0;i<size;i++){
                        MedicationInfoDatadb medicationInfoDatadb=new MedicationInfoDatadb(
                                1,
                                data.getList().get(i).getHasServedTime(),
                                data.getList().get(i).getDoseName(),
                                data.getList().get(i).getDetailId(),
                                data.getList().get(i).getMid(),
                                data.getList().get(i).getUseTime(),
                                data.getList().get(i).getId(),
                                data.getList().get(i).getUseCount(),
                                data.getList().get(i).getMName(),
                                data.getList().get(i).getStatus()
                        );
                        //将当日服药信息保存到数据库中
                        medicationInfoDatadb.save();
                    }
                }
            }

            @Override
            public void onError(String msg) {
                onMedicationListener.onError(msg);
            }

            @Override
            public void onRefreshUI() {
                onMedicationListener.onRefreshUI();
            }
        });
    }

    /**
     * 一种服药信息提交
     * @param status
     * @param id
     */
    public void AddOneTakeMedicineData(String status,String id,String detailId,String hasServedTime){
        requestModel.AddOneTakeMedicineData(status, id,detailId,hasServedTime, new AnotherResultListener<DataStateResult>() {
            @Override
            public void onSuccess(DataStateResult data) {
                ViseLog.d("一种服药信息提交onSuccess"+data);
                onMedicationListener.AddOneTakeMedicine(data);
            }

            @Override
            public void onError(String msg) {
                onMedicationListener.onError(msg);
                ViseLog.d("一种服药信息提交onError"+msg);
            }
            @Override
            public void onRefreshUI() {
                onMedicationListener.onRefreshUI();
            }
        });
    }

    /**
     * 一键服药信息提交
     * @param detailId
     */
    public void AddMutiTakeMedicineData(String detailId){
        requestModel.AddMutiTakeMedicineData(detailId,  new AnotherResultListener<DataStateResult>() {
            @Override
            public void onSuccess(DataStateResult data) {
                onMedicationListener.onAddMutiTakeMedicine(data);
            }

            @Override
            public void onError(String msg) {
                onMedicationListener.onError(msg);
            }

            @Override
            public void onRefreshUI() {
                onMedicationListener.onRefreshUI();
            }
        });
    }

    /**
     * 查询服药记录日期查询
     * @param startTime
     * @param endTime
     */
    public void MedicationRecordData(String startTime,String endTime){

        requestModel.MedicationRecordData(startTime, endTime, new AnotherResultListener<AnotherResult<MedicationRecordData>>() {
            @Override
            public void onSuccess(AnotherResult<MedicationRecordData> data) {
                onMedicationListener.onSuccessMedicationRecordDate(data);
            }

            @Override
            public void onError(String msg) {
                onMedicationListener.onError(msg);
            }

            @Override
            public void onRefreshUI() {
                onMedicationListener.onRefreshUI();
            }
        });
    }

    /**
     * 查询上次服药信息
     */

    public void GetlastPatientDoseIndexData(String toDay){

        requestModel.GetlastPatientDoseIndexData(toDay, new AnotherResultListener<AnotherResult<OnedayMedicationData>>() {
            @Override
            public void onSuccess(AnotherResult<OnedayMedicationData> data) {
                onMedicationListener.OnSeccessLast(data);
            }

            @Override
            public void onError(String msg) {
                onMedicationListener.onError(msg);
            }

            @Override
            public void onRefreshUI() {
                onMedicationListener.onRefreshUI();
            }
        });
    }

    /**
     * 查询下次服药信息
     */
    public void GetNextPatientDoseIndexData(String toDay){

        requestModel.GetNextPatientDoseIndexData(toDay, new AnotherResultListener<AnotherResult<OnedayMedicationData>>() {
            @Override
            public void onSuccess(AnotherResult<OnedayMedicationData> data) {
                onMedicationListener.OnSeccessNext(data);
            }

            @Override
            public void onError(String msg) {
                onMedicationListener.onError(msg);
            }

            @Override
            public void onRefreshUI() {
                onMedicationListener.onRefreshUI();
            }
        });
    }

}
